home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / DELPHI / COMPRESS.ZIP / BLOBDEMO.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-04-02  |  5.7 KB  |  158 lines

  1. (* BLOBDEMO.PAS V2.0
  2.  
  3.    This unit provides example code for two things:
  4.    1. On-the-fly creation of compressed blob fields
  5.    2. Writing and reading of any kind of data (e.g. arrays, AVI, WAV etc)
  6.       to/from a compressed blob field.
  7.  
  8.    Before running the program, you need to use the Database Desktop to create
  9.    a table called BLOB.DB in the DBDEMOS alias which contains a 'Name' field (A10)
  10.    and a 'Data' field (B0). For a full test, you'd also want to add routines
  11.    to put meaningful data into the OurData array, and display it
  12.    before/after blob reads.  We're just compressing a bunch of zeros...
  13.  
  14. *)
  15. unit Blobdemo;
  16.  
  17. interface
  18.  
  19. uses
  20.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  21.   Forms, Dialogs, StdCtrls, Grids, DBGrids, ExtCtrls, DBCtrls, DB, DBTables,
  22.   Compress, CompCtrl;  { <--- CompCtrl for the TCBlobField/TCBlobstream types }
  23.  
  24. type
  25.   TForm1 = class(TForm)
  26.     Table1: TTable;
  27.     DataSource1: TDataSource;
  28.     DBGrid1: TDBGrid;
  29.     SaveArray: TButton;
  30.     LoadArray: TButton;
  31.     Compress1: TCompress;
  32.     Table1Name: TStringField;
  33.     Label1: TLabel;
  34.     procedure FormCreate(Sender: TObject);
  35.     procedure FormDestroy(Sender: TObject);
  36.     procedure SaveArrayClick(Sender: TObject);
  37.     procedure LoadArrayClick(Sender: TObject);
  38.   private
  39.     { Private declarations }
  40.   public
  41.     { Public declarations }
  42.   end;
  43.  
  44. var
  45.   Form1: TForm1;
  46.   TestCount: Smallint; { just for making field names }
  47.  
  48. implementation
  49.  
  50. {$R *.DFM}
  51.  
  52. var  BlobDataField:TCBlobField;  { compressed field we'll set up at runtime }
  53.      OurData: Array[1..4000] of Smallint; { Data to go to/from the field }
  54.  
  55. { Here, we create our compressed field BEFORE opening the table. Note that
  56.   the field ('Data' in this case) should *not* appear in the list of fields
  57.   you see when you double-click on the Grid or on Table1. If it is, remove it.
  58.   If the list is blank, add whatever you need, but NOT 'Data'.            }
  59.  
  60. procedure TForm1.FormCreate(Sender: TObject);
  61. begin
  62.   BlobDataField := TCBlobField.Create(Self);
  63.   BlobDataField.CompressSource := Compress1; { Our TCompress component     }
  64.   BlobDataField.CompressionMethod := coRLE; { RLE compression, for example }
  65.   BlobDataField.FieldName:='Data'; { or whatever you call it in the table }
  66.   BlobDataField.Size := 0;         { The # of bytes stored WITHIN the table }
  67.   BlobDataField.DataSet := Table1;
  68.   try
  69.     Table1.Open;  { Note: In V2.0, we've edited our grid (double-click) to ensure
  70.                   that the new field will NOT be added to the grid. This is
  71.                   because the V2.0 grids try to detail with (and display) it as
  72.                   a TBlobfield instead of a TCBlobfield, which causes spurious
  73.                    "Blob not open" errors. Please adopt the same tactic if you
  74.                    are working with custom blob fields like this in Delphi 2.
  75.                  }
  76.   except
  77.  
  78. (* This code here would be FINE except that for some reason,
  79.    telling it to create a table with a ftBlob field size zero
  80.    (something perfectly possible in Database Desktop) results
  81.    in a table with a ftBlob ('B') field size 1. This is a baaad
  82.    thing and doesn't work for our purposes. So alas, we have to ask you
  83.    to create the demo table manually. Sorry about that. It's a pain.
  84.  
  85.     with Table1 do
  86.     begin
  87.       DatabaseName := 'DBDEMOS';
  88.       TableName := 'BLOB.DB';
  89.       TableType := ttParadox;
  90.       with FieldDefs do
  91.       begin
  92.         Clear;
  93.         Add('Name', ftString, 10,True);
  94.         Add('Data', ftBlob, 0,False);  { bzzzt -- wrong -- makes size 1, dammit! }
  95.       end;
  96.       IndexDefs.Clear;
  97.       CreateTable;
  98.       FieldDefs.Clear;
  99.       temptable.free;
  100.       Table1.Open; { open should work this time... but won't, due to above "spec" }
  101.     end;
  102. *)      { so instead, we: }
  103.      showMessage('Please create a table called BLOB.DB in the'+#13+
  104.                  'DBDEMOS alias, according to the specs in BLOBDEMO.PAS.'+#13+#13+
  105.                  'THEN run this program again.');
  106.  
  107.   end;
  108. end;
  109.  
  110.  
  111.  
  112. { After all that, here's the enjoyable bits.... }
  113.  
  114. { How to write/compress our array data to the blob }
  115. procedure TForm1.SaveArrayClick(Sender: TObject);
  116. var cbs: TCBlobstream;
  117. begin
  118.      { this is cosmetic stuff }
  119.   Inc(TestCount);
  120.   Table1.append; { put a new record in, what the heck (or you could just Edit) }
  121.   Table1.FieldByName('Name').asstring := 'Test '+IntToStr(TestCount);
  122.  
  123.     { this is the IMPORTANT bit }
  124.   cbs:= TCBlobstream.create(BlobDataField,bmWrite); { will save our data to it }
  125.   cbs.writeBuffer(OurData,sizeof(OurData));         { standard stream method   }
  126.   showmessage('Compressed and wrote '+IntToStr(cbs.size)+' bytes');
  127.   cbs.free;    { done! (compression occurs just here...) }
  128.   Table1.Post;
  129. end;
  130. { Note: If the data was coming from a file or another object, you could use the
  131.         CopyFrom method from the applicable filestream/memorystream/blobstream }
  132.  
  133.  
  134. { How to read/expand our data (of whatever nature) back into our array }
  135. procedure TForm1.LoadArrayClick(Sender: TObject);
  136. var cbs: TCBlobstream;
  137.     bs: TBlobStream;
  138. begin
  139.   cbs:= TCBlobstream.create(BlobDataField,bmRead); { will read our data from it }
  140.   cbs.readBuffer(OurData,sizeof(OurData));         { standard stream method   }
  141.   showmessage('Expanded and read '+IntToStr(cbs.size)+' bytes');
  142.   cbs.free;
  143.  
  144.   { Oh, and by the way... }
  145.   bs := TBlobStream.Create(BlobDataField,bmread); { a handle on our RAW (compressed) data }
  146.   showmessage('By the way, that was stored in only '+IntToStr(bs.size)+' bytes');
  147.   bs.free;
  148. end;
  149.  
  150. { Cleanup code }
  151. procedure TForm1.FormDestroy(Sender: TObject);
  152. begin
  153.   if Table1.Active then Table1.close;
  154.   BlobDataField.free;
  155. end;
  156.  
  157. end.
  158.